import 'es6-promise/auto'
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import {sync} from 'vuex-router-sync'
import FastClick from 'fastclick'
import http from './utils/http'
import './core/filter'
// 注册所有封装的组件
import tsUI from './components/index'
import {Toast} from 'mint-ui'

import './assets/iconfont/iconfont'
import PullTo from 'vue-pull-to'

import transfer from './core/transfer'

Vue.prototype.$toast = Toast
Vue.prototype.$http = http
Vue.config.productionTip = false

Vue.use(tsUI);

Vue.component('PullTo', PullTo);

sync(store, router)

//因beforeRouteEnter钩子不能使用this，所以挂载到window对象
window.$prod = process.env.NODE_ENV === 'production'

// add cordova.js 通过file://或者生产模式
if (window.location.protocol === 'file:' || $prod) {
  let cordovaScript = document.createElement('script')
  cordovaScript.setAttribute('type', 'text/javascript')
  cordovaScript.setAttribute('src', 'cordova.js')
  document.body.appendChild(cordovaScript)
}

if ($prod) {
  window.document.addEventListener('DOMContentLoaded', () => {
    FastClick.attach(document.body);
  }, false);
}
const vm = {
  el: '#app',
  store,
  router,
  // template: '<App/>',
  // components: {App}
  render: h => h(App)
};
window.$intercept = false;
if (window.navigator.onLine === true) {
}else {
  Toast('当前网络较慢或未连接')
  // return
}
if ($prod) {
  //cordova环境加载完后
  window.document.addEventListener('deviceready', () => {
    //实例化全局Vue
    new Vue(vm)
    let exitAppTicker = 0;
    window.document.addEventListener("backbutton", () => {
      //如果$intercept不为true (初始化值 false)
      if (!window.$intercept) {
        let currentPage = router.history.current.fullPath;
        const pages = ['/order', '/profile', '/', '/index'];
        if (pages.indexOf(currentPage) !== -1) {
          if (exitAppTicker === 0) {
            exitAppTicker++;
            Toast({
              message: "再点一次退出",
              duration: 1500,
              position: 'bottom'
            });
            setTimeout(() => {
              exitAppTicker = 0;
            }, 2000);
          } else if (exitAppTicker === 1) {
            //退出app
            navigator.app.exitApp();
          }
        } else if (currentPage === '/profile/info') {
          // transfer.$emit('currBack');
          return false
        } else if (currentPage === '/register/pending') {
          router.replace({name: 'login'})
        }
        else if (currentPage === '/register/addProfile') {
          return false;
        } else if (currentPage === '/login') {
          //退出app
          navigator.app.exitApp();
        } else {
          router.goBack();
        }
      }
    }, false);
  }, false)
} else {
  new Vue(vm)
}

